#include "stdafx.h"
#include "wf_modifymoverotate.h"

#include "pf_snapper.h"



WF_ModifyMoveRotate::WF_ModifyMoveRotate(
    PF_Container& container,
    PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Move and Rotate Entities",
                           container, graphicView) {
}

QAction* WF_ModifyMoveRotate::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	// tr("Move and Rotate")
    QAction* action = new QAction(tr("M&ove and Rotate"), NULL);
	action->setIcon(QIcon(":/extui/modifymoverotate.png"));
	//action->zetStatusTip(tr("Move and Rotate Entities"));
    return action;
}

void WF_ModifyMoveRotate::init(int status) {
    PF_ActionInterface::init(status);
}



void WF_ModifyMoveRotate::trigger() {

    PF_DEBUG->print("PF_ActionModifyMoveRotate::trigger()");

    PF_Modification m(*container, graphicView);
    m.moveRotate(data);

    finish();

    PF_DIALOGFACTORY->updateSelectionWidget(container->countSelected());
}



void WF_ModifyMoveRotate::mouseMoveEvent(PF_MouseEvent* e) {
    PF_DEBUG->print("PF_ActionModifyMoveRotate::mouseMoveEvent begin");

    if (getStatus()==SetReferencePoint ||
            getStatus()==SetTargetPoint) {

        PF_Vector mouse = snapPoint(e);
        switch (getStatus()) {
        case SetReferencePoint:
            data.referencePoint = mouse;
            break;

        case SetTargetPoint:
            if (data.referencePoint.valid) {
                targetPoint = mouse;
                data.offset = targetPoint-data.referencePoint;

                deletePreview();
                preview->addSelectionFrom(*container);
                preview->rotate(data.referencePoint, data.angle);
                preview->move(data.offset);
                drawPreview();
            }
            break;

        default:
            break;
        }
    }

    PF_DEBUG->print("PF_ActionModifyMoveRotate::mouseMoveEvent end");
}



void WF_ModifyMoveRotate::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        PF_CoordinateEvent ce(snapPoint(e));
        coordinateEvent(&ce);
    } else if (e->button()==Qt::RightButton) {
        deletePreview();
        init(getStatus()-1);
    }
}



void WF_ModifyMoveRotate::coordinateEvent(PF_CoordinateEvent* e) {
    if (e==NULL) {
        return;
    }

    PF_Vector pos = e->getCoordinate();

    switch (getStatus()) {
    case SetReferencePoint:
        data.referencePoint = pos;
        setStatus(SetTargetPoint);
        break;

    case SetTargetPoint:
        targetPoint = pos;

        setStatus(ShowDialog);
        data.offset = targetPoint - data.referencePoint;
        if (PF_DIALOGFACTORY->requestMoveRotateDialog(data)) {
            trigger();
            //finish();
        }
        break;

    default:
        break;
    }
}


void WF_ModifyMoveRotate::commandEvent(PF_CommandEvent* e) {
    PF_String c = e->getCommand().lower();

    if (checkCommand("help", c)) {
        PF_DIALOGFACTORY->commandMessage(msgAvailableCommands()
                                         + getAvailableCommands().join(", "));
        return;
    }

    switch (getStatus()) {
    case SetReferencePoint:
    case SetTargetPoint:
		//   changed from if (c==checkCommand("angle", c)) {
        if (checkCommand("angle", c)) {
            deletePreview();
            lastStatus = (Status)getStatus();
            setStatus(SetAngle);
        }
        break;

    case SetAngle: {
            bool ok;
            double a = PF_Math::eval(c, &ok);
            if (ok==true) {
                data.angle = PF_Math::deg2rad(a);
            } else {
                PF_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
            }
            PF_DIALOGFACTORY->requestOptions(this, true, true);
            setStatus(lastStatus);
        }
        break;
    }
}



PF_StringList WF_ModifyMoveRotate::getAvailableCommands() {
    PF_StringList cmd;

    switch (getStatus()) {
    case SetReferencePoint:
    case SetTargetPoint:
        cmd += command("angle");
        break;

    default:
        break;
    }

    return cmd;
}



void WF_ModifyMoveRotate::showOptions() {
    //std::cout << "PF_ActionModifyMoveRotate::showOptions()\n";

    PF_ActionInterface::showOptions();

    PF_DIALOGFACTORY->requestOptions(this, true);
}



void WF_ModifyMoveRotate::hideOptions() {
    //std::cout << "PF_ActionModifyMoveRotate::hideOptions()\n";

    PF_ActionInterface::hideOptions();

    PF_DIALOGFACTORY->requestOptions(this, false);
}


void WF_ModifyMoveRotate::updateMouseButtonHints() {
    switch (getStatus()) {
    case SetReferencePoint:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Specify reference point"),
                                            tr("Cancel"));
        break;
    case SetTargetPoint:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Specify target point"),
                                            tr("Back"));
        break;
    case SetAngle:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Enter rotation angle:"),
                                            tr("Back"));
        break;
    default:
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}



void WF_ModifyMoveRotate::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_ModifyMoveRotate::updateToolBar() {
    switch (getStatus()) {
    case SetReferencePoint:
    case SetTargetPoint:
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
        break;
    default:
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarModify);
        break;
    }
}


// EOF
